/**
 * Copyright 2010 Per-Erik Bergman (per-erik.bergman@jayway.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.jayway.opengl.tutorial;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import se.jayway.opengl.tutorial.mesh.Cube;
import se.jayway.opengl.tutorial.mesh.Group;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class OpenGLRenderer implements Renderer {
	private final Group root;
	// Our vertex buffer.
	private final FloatBuffer mVerticesBuffer = null;
	int mWidth, mHeight;
	long startTime = System.nanoTime();
	int mTextureId;

	public OpenGLRenderer(int width, int height) {
		// Initialize our root.
		root = new Group();
		mWidth = width;
		mHeight = height;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition
	 * .khronos.opengles.GL10, javax.microedition.khronos.egl.EGLConfig)
	 */
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// // Set the background color to black ( rgba ).
		// gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		// // Enable Smooth Shading, default not really needed.
		// gl.glShadeModel(GL10.GL_SMOOTH);
		// // Depth buffer setup.
		// gl.glClearDepthf(1.0f);
		// // Enables depth testing.
		// gl.glEnable(GL10.GL_DEPTH_TEST);
		// // The type of depth testing to do.
		// gl.glDepthFunc(GL10.GL_LEQUAL);
		// gl.glClearDepthf(GL10.GL_ONE);
		// // Really nice perspective calculations.
		// gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		// SimplePlane plane = new SimplePlane();
		Cube cube = new Cube();
		cube.loadBitmap(gl, BitmapFactory.decodeResource(MyApplication
				.getContext().getResources(), R.drawable.jay));
		mTextureId = cube.loadBitmap(gl, BitmapFactory.decodeResource(
				MyApplication.getContext().getResources(), R.drawable.jay));
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
				GL10.GL_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
				GL10.GL_NEAREST);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
		root.add(cube);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.
	 * khronos.opengles.GL10)
	 */
	public void onDrawFrame(GL10 gl) {
		// gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		// gl.glViewport(0, 0, mWidth, mHeight);
		// gl.glMatrixMode(GL10.GL_PROJECTION);
		// gl.glLoadIdentity();
		// gl.glOrthof(-1, 1, -1, 1, 10, -10);
		// gl.glMatrixMode(GL10.GL_MODELVIEW);
		// gl.glLoadIdentity();

		// gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		// gl.glViewport(0, 0, mWidth, mHeight);
		// gl.glMatrixMode(GL10.GL_PROJECTION);
		// gl.glLoadIdentity();
		// GLU.gluPerspective(gl, 67, mWidth / (float) mHeight, 0.1f, 10f);
		// gl.glMatrixMode(GL10.GL_MODELVIEW);
		// gl.glLoadIdentity();
		float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
		startTime = System.nanoTime();

		root.update(deltaTime);

		gl.glViewport(0, 0, mWidth, mHeight);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 67, mWidth / (float) mHeight, 0.1f, 10f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();

		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureId);

		// Draw our scene.
		root.draw(gl);
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisable(GL10.GL_DEPTH_TEST);

		// gl.glLoadIdentity();
		// gl.glTranslatef(3, 0, 3);
		// gl.glScalef(2, 2, 2);
		// root.draw(gl);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition
	 * .khronos.opengles.GL10, int, int)
	 */
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		mWidth = width;
		mHeight = height;
		// // Sets the current view port to the new size.
		// gl.glViewport(0, 0, width, height);
		// // Select the projection matrix
		// gl.glMatrixMode(GL10.GL_PROJECTION);
		// // Reset the projection matrix
		// gl.glLoadIdentity();
		// // Calculate the aspect ratio of the window
		// GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 1.0f,
		// 100.0f);
		// // GLU.gluPerspective(gl, 45.0f, (float) width / (float) height,
		// 0.1f,
		// // 1000.0f);
		// GLU.gluLookAt(gl, 10, 10, 10, 0, 0, 0, 0, 1, 0);
		//
		// // Select the modelview matrix
		// gl.glMatrixMode(GL10.GL_MODELVIEW);
		// // Reset the modelview matrix
		// gl.glLoadIdentity();
	}

}
